import _ from 'lodash';
import {
    URLHEAD,
    SHP_PATH,
    POI_IMAGE_RULE
} from '@/config/global'

export class Camera {
    constructor(option) {
        this.prototype = {
            centerCoord: '0,0',
            elevation: 0,
            pitch: 0,
            yaw: 0,
            elevationRange: '0,10000',
            pitchRange: '10,89',
            yawRange: '0,300',
            coord_z_type: 0,
            points: [],
            cargo: null
        }
        this.data = Object.assign(this.prototype, option);
        if (option) {
            cloudRender.SuperAPI('SetCameraSpace', {
                coord_type: this.data.coord_type,
                cad_mapkey: this.data.cad_mapkey,
                coord_z: this.data.elevation,
                coord_z_type: this.data.coord_z_type,
                arm_distance_limit: this.data.elevationRange,
                pitch_limit: this.data.pitchRange,
                yaw_limit: this.data.yawRange,
                points: this.data.points
            });
            cloudRender.SuperAPI('SetCameraRotate', {
                time: 200,
                direction: 'clockwise'
            })
        }


    }
    set params(value) {

        this.data.centerCoord = value.centerCoord;
        this.data.elevation = value.elevation;
        this.data.pitch = value.pitch;
        this.data.yaw = value.yaw;
        if (value !== null) {
            cloudRender.SuperAPI('SetCameraInfo', {
                coord_type: 0,
                cad_mapkey: '',
                center_coord: value.centerCoord,
                yaw: value.yaw,
                pitch: value.pitch,
                arm_distance: value.elevation,
                fly: false,
            });
        }
    }

    get params() {
        return {
            centerCoord: this.data.centerCoord,
            elevation: this.data.elevation,
            pitch: this.data.pitch,
            yaw: this.data.yaw
        }
    }

    setOn(value) {
        this.params = value;
    }

    rotateStart() {
        cloudRender.SuperAPI('SetCameraRotate', {
            time: 200,
            direction: 'clockwise'
        })
    }

    rotateStop() {
        cloudRender.SuperAPI('SetCameraRotate', {
            time: 200,
            direction: 'stop'
        })
    }

    focusOn(eleType) {
        let hash = {
            'POI': 'FocusAllPOI',
            'path': 'FocusAllPath',
            'range': 'FocusAllRange',
            'heatmap': 'FocusAllHeatMap',
            'roadheatmap': 'FocusAllRoadHeatMap',
            'migrationmap': 'FocusAllMigrationMap'
        };
        setTimeout(() => {
            cloudRender.SuperAPI(hash[eleType]);
            cloudRender.SuperAPI('SetCameraRotate', {
                time: 200,
                direction: 'clockwise'
            })
        }, 200)
        return this;
    }

    clear(typeName) {
        let hash = {
            'POI': 'RemoveAllPOI',
            'path': 'RemoveAllPath',
            'range': 'RemoveAllRange',
            'heatmap': 'RemoveAllHeatMap',
            'roadheatmap': 'RemoveAllRoadHeatMap',
            'migrationmap': 'RemoveAllMigrationMap'
        };
        if (!typeName) {
            cloudRender.SuperAPI('RemoveAllPOI');
            cloudRender.SuperAPI('RemoveAllPath');
            cloudRender.SuperAPI('RemoveAllRange');
            cloudRender.SuperAPI('RemoveAllHeatMap');
            cloudRender.SuperAPI("RemoveAllColumnHeatMap");
            cloudRender.SuperAPI("RemoveAllSpaceHeatMap")
            cloudRender.SuperAPI("RemoveAllRoadHeatMap")
            cloudRender.SuperAPI("RemoveAllMigrationMap")
            cloudRender.SuperAPI("RemoveAllStrategyMap")
            cloudRender.SuperAPI('RemoveAllEffect');
        } else {
            cloudRender.SuperAPI(hash[typeName]);
        }
        return this;

    }

    
    measureTool(value){
        const hashMap = {
            'on':'StartMeasureTool',
            'off':'EndMeasureTool'
        }
        cloudRender.SuperAPI(hashMap[value],{
            coord_type:0,
            cad_mapkey:''
        })
    }
}

export class POI {
    constructor(option) {
        this.prototype = {
            id: 'poi-id',
            coord_type: 0,
            coord_z_type: 0,
            cad_mapkey: null,
            iconName: 'default',
            labelName: null,
            elevation: 1,
            state: 'normal',
            title: null,
            windowUrl: null,
            windowSize: '0,0',
            coord: null,
            bindOn: null,
            viewMode: "window",
            viewName: null,
            cargo: null,
            always_show_label: true
        }
       
        this.data = Object.assign(this.prototype, option);
        this.cargo = this.data.cargo;
        let poiJson = {
            id: this.data.id,
            coord_type: this.data.coord_type,
            cad_mapkey: this.data.cad_mapkey,
            coord: this.data.coord,
            coord_z: this.data.elevation,
            coord_z_type: this.data.coord_z_type,
            always_show_label: this.data.always_show_label,
            show_label_range: '0,2000',
            sort_order: false,
            state: this.data.state,
            marker: {
                size: POI_IMAGE_RULE.markerSize(this.data.iconName).normal,
                images: POI_IMAGE_RULE.markerImages(this.data.iconName)
            },
            label: this.data.title ? {
                ...POI_IMAGE_RULE.labelImages(this.data.labelName),
                content: [{
                    "text": [this.data.title, 'ffffffff', 36],
                    "text_offset": "36,4" //文本框左上角相对于label中bg_offset左上角的margin偏移(x,y 单位:像素), 注: x为正向右, y为正向下
                }]
            } : {
                bg_image_url: '',
                bg_size: "0,0",
                bg_offset: "0,0",
                text: [],
                text_offset: '0,0'
            },
            window: this.data.windowUrl ? {
                url: this.data.windowUrl,
                size: this.data.windowSize,
                offset: '35,60'
            } : {
                url: 'http://',
                size: '1,1',
                offset: '0,0'
            }
        }
        cloudRender.SuperAPI('AddCustomPOI', poiJson);
        console.dir(poiJson);
    }

    get id() {
        return this.data.id
    }

    set coord(value) {
        let $this = this;
        $this.data.coord = value;
        cloudRender.SuperAPI('UpdateCustomPOICoord', {
            id: $this.data.id,
            coord_type: $this.data.coord_type,
            cad_mapkey: $this.data.cad_mapkey,
            coord: value,
            coord_z: $this.data.elevation
        });
        return this;
    }

    get coord() {
        return this.data.coord;
    }

    set elevation(value) {
        let $this = this;
        $this.data.elevation = value;
        cloudRender.SuperAPI('UpdateCustomPOICoord', {
            id: $this.data.id,
            coord_type: $this.data.coord_type,
            cad_mapkey: $this.data.cad_mapkey,
            coord: $this.data.coord,
            coord_z: value
        });
        return this;
    }

    get elevation() {
        return this.data.elevation;
    }

    get iconName() {
        return this.data.iconName;
    }

    set state(value) {
        let $this = this;
        $this.data.state = value;
        cloudRender.SuperAPI('UpdateCustomPOIStyle', {
            id: $this.data.id,
            always_show_label: true,
            show_label_range: '0,2000',
            sort_order: false,
            state: value
        });
        return this;
    }

    set window(value = {
        url: '',
        size: '',
        offset: '30,200'
    }) {
        let $this = this;
        $this.data.window = value;
        cloudRender.SuperAPI('UpdateCustomPOIWindow', {
            url: value.url,
            size: value.size,
            offset: value.offset
        });
        return this;
    }

    get windowUrl() {
        return this.data.windowUrl;
    }

    get windowSize() {
        return this.data.windowSize;
    }

    get viewMode() {
        return this.data.viewMode;
    }

    get viewName() {
        return this.data.viewName;
    }

    get bindOn() {
        return this.data.bindOn;
    }

    //public methods
    focus(distance = 200) {
        cloudRender.SuperAPI('FocusPOI', {
            id: this.data.id,
            distance: distance
        })
    }

    remove() {
        cloudRender.SuperAPI('RemovePOI', {
            id: this.data.id
        })
    }

    moveByScript(pointList) {
        if (pointList instanceof Array) {
            let pathPoints = [];
            let headNode = null;
            const poi = this;
            let path = new Path({
                id: poi.id + '-coverage-path',
                shape: 'scan_line',
                color: "#33ff00",
                width: 15,
                points: []
            });
            const autoTickFn = () => {
                headNode = pointList.shift();
                // poi.coord = headNode;
                pathPoints.push(headNode);
                path.coords = pathPoints;
                cloudRender.SuperAPI("CoverToMove", {
                    attach_id: poi.id,
                    attach_type: 'poi',
                    be_attach_id: path.id,
                    be_attach_type: 'path',
                    speed: 5,
                    loop: false,
                    reverse: false
                })
            }
            autoTickFn();
            const autoTick = setInterval(() => {
                if (pointList.length === 0) {
                    clearInterval(autoTick);
                } else {
                    autoTickFn();
                }
            })
        }
        return this;
    }

    migrateByScript(pointEnd, pointList) {
        if (pointList instanceof Array) {
            let headNode = null;
            let poiStart = this;
            let migrationMap = new MigrationMap({
                id: poiStart.id + '-migration-map',
                color: '#33ff00',
                bindOn: 'matched',
                width: 15,
                start: {
                    coord: poiStart.coord,
                    elevation: poiStart.elevation
                },
                ends: [{
                    coord: pointEnd,
                    elevation: poiStart.elevation
                }]
            })
            migrationMap.focus();
            const autoTickFn = () => {
                headNode = pointList.shift();
                poiStart.coord = headNode;
            }
            autoTickFn();
            const autoTick = setInterval(() => {
                if (pointList.length === 0) {
                    clearInterval(autoTick);
                } else {
                    autoTickFn();
                }
            }, 1000)
        }
        return this;
    }
}

export class Heatmap {
    constructor(option) {
        this.prototype = {
            id: "heatmap-id",
            area: {
                lt: '0,0',
                lb: '0,0',
                rt: '0,0',
                rb: '0,0'
            },
            points: [{
                coord: '0,0',
                value: 0
            }],
            diameter: 10,
            range: '0,100',
            bindOn: ''
        };
        this.data = Object.assign(this.prototype, option);
        this.cargo = option.cargo;
        let delay = 0;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        let json = {
            id: this.data.id,
            coord_type: 0,
            cad_mapkey: '',
            heatmap_type: 1,
            leftupper_coord: this.data.area.lt,
            leftlower_coord: this.data.area.lb,
            rightlower_coord: this.data.area.rb,
            rightupper_coord: this.data.area.rt,
            coord_z: 1,
            brush_diameter: this.data.diameter,
            heatpoint_minvalue: this.data.range.split(',')[0],
            heatpoint_maxvalue: this.data.range.split(',')[1],
            data: this.data.points
        }
        setTimeout(() => {
            cloudRender.SuperAPI('AddHeatMap', json)
        }, delay)
        let timeCost = this.data.points.length * 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);
        return this;
    }

    get id() {
        return this.data.id;
    }

    set heatData(value) {
        this.data.points = value;
        let delay = 0;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        setTimeout(() => {
            cloudRender.SuperAPI('UpdateHeatMapCoord', {
                id: this.id,
                coord_type: 0,
                cad_mapkey: '',
                coord_z: 1,
                data: value,
                clear_old_data: true
            });
        }, delay)
        let timeCost = this.data.points.length * 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);

    }

    get heatData() {
        return this.data.points;
    }

    set range(value) {
        this.data.range = value;
        cloudRender.SuperAPI('UpdateHeatMapStyle', {
            id: this.data.id,
            heatmap_type: 1,
            brush_diameter: this.data.diameter,
            heatpoint_minvalue: value.split(',')[0],
            heatpoint_maxvalue: value.split(',')[1]
        })
    }

    get range() {
        return this.data.range;
    }

    set diameter(value) {
        this.data.diameter = value;
        cloudRender.SuperAPI('UpdateHeatMapStyle', {
            id: this.data.id,
            heatmap_type: 1,
            brush_diameter: value,
            heatpoint_minvalue: this.data.range.split(',')[0],
            heatpoint_maxvalue: this.data.range.split(',')[1]
        })
    }

    get diameter() {
        return this.data.diameter;
    }

    addPoints(value) {
        let delay = 0;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        this.data.points = this.data.points.concat(value);
        setTimeout(() => {
            cloudRender.SuperAPI('UpdateHeatMapCoord', {
                id: this.id,
                coord_type: 0,
                cad_mapkey: '',
                coord_z: 1,
                data: value,
                clear_old_data: false
            });
        }, delay)
        let timeCost = value.length * 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);
        return this;
    }

    focus(distance = 200) {
        cloudRender.SuperAPI('FocusHeatMap', {
            id: this.id,
            distance: distance
        })
    }

    remove() {
        cloudRender.SuperAPI('RemoveHeatMap', {
            id: this.id
        })
    }
}

export class Roadheatmap {
    constructor(option) {
        this.prototype = {
            id: "default-roadmap-id",
            coord_type: 0,
            coord_z_type: 0,
            cad_mapkey: '',
            width: 10,
            levelRules: [{
                level: 1,
                color: '#009900'
            }],
            points: [{
                coord: "0,0",
                elevation: 0,
                level: 1
            }],
            bindOn: ''
        }
        this.data = Object.assign(this.prototype, option);
        let delay = 0;
        let $this = this;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        setTimeout(() => {
            cloudRender.SuperAPI('AddRoadHeatMap', {
                id: $this.data.id,
                coord_type: $this.data.coord_type,
                coord_z_type: $this.data.coord_z_type,
                cad_mapkey: $this.data.cad_mapkey,
                width: $this.data.width,
                roadheatmap_define: $this.data.levelRules.map(o => {
                    return {
                        level: o.level || 1,
                        color: o.color.replace('#', '')
                    }
                }),
                points: $this.data.points.map(o => {
                    return {
                        coord: o.coord,
                        coord_z: o.elevation,
                        level: o.level
                    }
                })
            })
        }, delay)
        let timeCost = this.data.points.length * 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);
        return this;
    }

    get id() {
        return this.data.id;
    }

    get points() {
        return this.data.points;
    }

    set points(val = [{
        coord: '0,0',
        elevation: 0,
        level: 1
    }]) {
        let newPoints = val.map(o => {
            return {
                coord: o.coord || "0,0",
                coord_z: o.elevation || 0,
                level: o.level || 1
            }
        });
        this.data.points = newPoints;
        cloudRender.SuperAPI('UpdateRoadHeatMapCoord', {
            id: this.data.id,
            coord_type: this.data.coord_type,
            cad_mapkey: this.data.cad_mapkey,
            coord_z_type: this.data.coord_z_type,
            points: newPoints
        });
        return this;
    }

    get width() {
        return this.data.width;
    }

    set width(value = 10) {
        this.data.width = value;
        cloudRender.SuperAPI('UpdateRoadHeatMapStyle', {
            id: this.data.id,
            width: value,
            roadheatmap_define: this.data.levelRules.map(o => {
                return {
                    level: o.level || 1,
                    color: o.color.replace('#', '')
                }
            })
        })
        return this;
    }

    get levelRules() {
        return this.data.levelRules;
    }

    set levelRules(value = [{
        level: 1,
        color: '#005500'
    }]) {
        let newRules = value.map(o => {
            return {
                level: o.level,
                color: o.color.replace('#', '')
            }
        })
        this.data.levelRules = newRules;
        cloudRender.SuperAPI('UpdateRoadHeatMapStyle', {
            id: this.data.id,
            width: this.data.width,
            roadheatmap_define: newRules
        })
        return this;
    }
    focus(distance = 200) {
        cloudRender.SuperAPI('FocusRoadHeatMap', {
            id: this.data.id,
            distance: distance
        })
    }

    remove() {
        cloudRender.SuperAPI('RemoveRoadHeatMap', {
            id: this.data.id
        })
    }
}

export class MigrationMap {
    constructor(option) {
        const shapeHash = {
            "wave": 1,
            "arrow": 2,
            "ray": 3
        }
        this.prototype = {
            id: "migration-id",
            coord_type: 0,
            cad_mapkey: '',
            shape: 'arrow',
            color: '#000000',
            width: 10,
            curvature: 0.5,
            start: {
                coord: '0,0',
                elevation: 0
            },
            ends: [{
                coord: '0,0',
                elevation: 0
            }],
            bindOn: ''
        }
        this.cargo = option.cargo;
        this.data = Object.assign(this.prototype, option);
        let delay = 0;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        setTimeout(() => {
            cloudRender.SuperAPI('AddMigrationMap', {
                id: this.data.id,
                coord_type: this.data.coord_type,
                coord_z_type: 0,
                cad_mapkey: this.data.cad_mapkey,
                type: shapeHash[this.data.shape],
                start_coord: this.data.start.coord,
                start_coord_z: this.data.start.elevation,
                coord_z_type: 0,
                targetdata: this.data.ends.map(o => {
                    return {
                        target_coord: o.coord,
                        target_coord_z: o.coord_z,
                        mark_size: this.data.width * 2,
                        mark_color: this.data.color.replace('#', ''),
                        line_width: this.data.width,
                        line_color: this.data.color,
                        curvature: this.data.curvature
                    }
                })
            });
        }, delay)
        let timeCost = (this.data.ends.length + 1) * 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);
        return this;
    }

    get id() {
        return this.data.id
    }

    get shape() {
        return this.data.shape;
    }

    set shape(value) {
        this.data.shape = value;
        const shapeHash = {
            "wave": 1,
            "arrow": 2,
            "ray": 3
        }
        let jsondata = {
            id: this.id,
            type: shapeHash[value],
            targetdata: this.data.ends.map(o => {
                return {
                    target_coord: o.coord,
                    target_coord_z: o.coord_z,
                    mark_size: this.data.width * 2,
                    mark_color: this.data.color.replace('#', ''),
                    line_width: this.data.width,
                    line_color: this.data.color.replace('#', ''),
                    curvature: this.data.curvature
                }
            })
        }
        cloudRender.SuperAPI('UpdateMigrationMapStyle', jsondata);
        return this;
    }

    get width() {
        return this.data.width;
    }

    set width(value) {
        this.data.width = value;
        const shapeHash = {
            "wave": 1,
            "arrow": 2,
            "ray": 3
        }
        let jsondata = {
            id: this.id,
            type: shapeHash[this.shape],
            targetdata: this.data.ends.map(o => {
                return {
                    target_coord: o.coord,
                    target_coord_z: o.coord_z,
                    mark_size: value * 2,
                    mark_color: this.data.color.replace('#', ''),
                    line_width: value,
                    line_color: this.data.color.replace('#', ''),
                    curvature: this.data.curvature
                }
            })
        }
        cloudRender.SuperAPI('UpdateMigrationMapStyle', jsondata);
        return this;
    }

    get color() {
        return this.data.color;
    }

    set color(value) {
        this.data.color = value;
        const shapeHash = {
            "wave": 1,
            "arrow": 2,
            "ray": 3
        }
        let jsondata = {
            id: this.id,
            type: shapeHash[this.shape],
            targetdata: this.data.ends.map(o => {
                return {
                    target_coord: o.coord,
                    target_coord_z: o.coord_z,
                    mark_size: this.data.width * 2,
                    mark_color: value.replace('#', ''),
                    line_width: this.data.width,
                    line_color: value.replace('#', ''),
                    curvature: this.data.curvature
                }
            })
        }
        cloudRender.SuperAPI('UpdateMigrationMapStyle', jsondata);
        return this;
    }

    get startCoord() {
        return {
            coord: this.data.start.coord,
            elevation: this.data.start.elevation
        }
    }

    set startCoord(value) {
        this.data.startCoord = value;
        let jsondata = {
            id: this.data.id,
            coord_type: this.data.coord_type,
            cad_mapkey: this.data.cad_mapkey,
            start_coord: value.coord,
            start_coord_z: value.elevation || 0,
            targetdata: this.data.ends.map(o => {
                return {
                    target_coord: o.coord,
                    target_coord_z: o.elevation
                }
            })
        }
        cloudRender.SuperAPI("UpdateMigrationMapCoord", jsondata)
        return this;
    }

    get endCoords() {
        return this.data.ends;
    }

    set endCoords(value) {
        this.data.endCoords = value;
        let jsondata = {
            id: this.data.id,
            coord_type: this.data.coord_type,
            cad_mapkey: this.data.cad_mapkey,
            start_coord: this.data.start.coord,
            start_coord_z: this.data.start.elevation,
            targetdata: value.map(o => {
                return {
                    target_coord: o.coord,
                    target_coord_z: o.elevation
                }
            })
        }
        cloudRender.SuperAPI("UpdateMigrationMapCoord", jsondata)
        return this;
    }

    focus(distance = 200) {
        cloudRender.SuperAPI('FocusMigrationMap', {
            id: this.id,
            distance: distance
        })
    }

    remove() {
        cloudRender.SuperAPI('RemoveMigrationMap', {
            id: this.id
        })
    }
}

export class Path {
    constructor(option) {
        this.prototype = {
            id: "path-id",
            coord_type: 0,
            cad_mapkey: "",
            shape: "solid",
            color: "#ff0000",
            width: 10,
            points: [],
            type: "",
            bindOn: "",
            poiList: []
        }
        this.data = Object.assign(this.prototype, option);
        this.cargo = option.cargo;
        //读取上一次的渲染时间消耗
        let delay = 0;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        setTimeout(() => {
            cloudRender.SuperAPI('AddPath', {
                id: this.data.id,
                coord_type: this.data.coord_type,
                coord_z_type: 0,
                cad_mapkey: this.data.cad_mapkey,
                type: this.data.shape,
                color: this.data.color.replace('#', ''),
                pass_color: this.data.color.replace('#', ''),
                width: this.data.width,
                points: this.data.points
            });
        }, delay)

        //写入本次路径渲染所需的预估时间，便于后续其他元素渲染时，作为一种延时参考
        //根据测算，路径每个点的渲染时间大概是3ms
        let timeCost = this.data.points.length * 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);
        return this;
    }

    get id() {
        return this.data.id;
    }

    set shape(value) {
        this.data.shape = value;
        let jsondata = {
            id: this.data.id,
            type: value,
            color: this.data.color,
            width: this.data.width
        }
        cloudRender.SuperAPI("UpdatePathStyle", jsondata);
        return this;
    }
    get shape() {
        return this.data.shape
    }

    set color(value) {
        this.data.color = value;
        let jsondata = {
            id: this.data.id,
            type: this.data.shape,
            color: value.replace('#', ''),
            width: this.data.width
        }
        cloudRender.SuperAPI("UpdatePathStyle", jsondata);
        return this;
    }
    get color() {
        return this.data.color
    }

    set width(value) {
        this.data.width = value;
        let jsondata = {
            id: this.data.id,
            type: this.data.shape,
            color: this.data.color,
            width: value
        }
        cloudRender.SuperAPI("UpdatePathStyle", jsondata);
        return this;
    }
    get width() {
        return this.data.width;
    }

    set coords(value) {
        this.data.points = value;
        let jsondata = {
            id: this.data.id,
            coord_type: this.data.coord_type,
            cad_mapkey: this.data.cad_mapkey,
            points: value
        }
        let delay = 0;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        setTimeout(() => {
            cloudRender.SuperAPI('UpdatePathCoord', jsondata);
        }, delay);
        let timeCost = value.length * 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);
        return this;
    }
    get coords() {
        return this.data.points;
    }

    get type() {
        return this.data.type;
    }

    focus(distance = 200) {
        cloudRender.SuperAPI('FocusPath', {
            id: this.id,
            distance: distance
        })
    }

    remove() {
        cloudRender.SuperAPI('RemovePath', {
            id: this.id
        })
    }
}

export class Range {
    constructor(option) {
        let prototype = {
            id: "range_id",
            coord_type: 0,
            cad_mapkey: "",
            coord_z_type: 0,
            barrierStyle: "wave",
            color: "#ff0000",
            height: 10,
            areaStyle: "none",
            points: [],
            bindOn: '',
            poiList: []
        }
        this.id = option.id;
        this.data = Object.assign(prototype, option);
        this.cargo = option.cargo;
        let delay = 0;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        //setTimeout(() => {
        cloudRender.SuperAPI('AddRange', {
            id: this.data.id,
            coord_type: this.data.coord_type,
            cad_mapkey: this.data.cad_mapkey,
            coord_z_type: this.data.coord_z_type,
            type: this.data.barrierStyle,
            color: this.data.color.replace('#', ''),
            range_height: this.data.height,
            fill_area: this.data.areaStyle,
            points: this.data.points
        })
        //}, delay)
        //写入本次路径渲染所需的预估时间，便于后续其他元素渲染时，作为一种延时参考
        //根据测算，路径每个点的渲染时间大概是3ms
        let timeCost = this.data.points.length * 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);
        return this;
    }

    set barrierStyle(value) {
        this.data.barrierStyle = value;
        let jsondata = {
            id: this.data.id,
            type: value,
            color: this.data.color.replace('#', ''),
            range_height: this.data.height,
            fill_area: this.data.areaStyle
        }
        cloudRender.SuperAPI('UpdateRangeStyle', jsondata);
        return this;
    }

    get barrierStyle() {
        return this.data.barrierStyle;
    }

    set color(value) {
        this.data.color = value;
        let jsondata = {
            id: this.data.id,
            type: this.data.barrierStyle,
            color: value.replace('#', ''),
            range_height: this.data.height,
            fill_area: this.data.areaStyle
        }
        cloudRender.SuperAPI('UpdateRangeStyle', jsondata);
        return this;
    }

    get color() {
        return this.data.color;
    }

    set areaStyle(value) {
        this.data.areaStyle = value;
        let jsondata = {
            id: this.data.id,
            type: this.data.barrierStyle,
            color: this.data.color.replace('#', ''),
            range_height: this.data.height,
            fill_area: value
        }
        cloudRender.SuperAPI('UpdateRangeStyle', jsondata);
        return this;
    }

    get areaStyle() {
        return this.data.areaStyle;
    }

    set height(value) {
        this.data.height = value;
        let jsondata = {
            id: this.data.id,
            type: this.data.barrierStyle,
            color: this.data.color.replace('#', ''),
            range_height: value,
            fill_area: this.data.areaStyle
        }
        cloudRender.SuperAPI('UpdateRangeStyle', jsondata);
        return this;
    }

    get height() {
        return this.data.height;
    }

    set points(value) {
        this.data.points = value;
        let jsondata = {
            id: this.data.id,
            coord_type: this.data.coord_type,
            cad_mapkey: this.data.cad_mapkey,
            coord_z_type: this.data.coord_z_type,
            points: value
        }
        let delay = 0;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        setTimeout(() => {
            cloudRender.SuperAPI('UpdateRangeCoord', jsondata);
        }, delay)
        let timeCost = this.data.points.length * 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);
        return this;
    }

    get points() {
        return this.data.points;
    }

    focus(distance = 200) {
        cloudRender.SuperAPI('FocusRange', {
            id: this.id,
            distance: distance
        })
    }

    remove() {
        cloudRender.SuperAPI('RemoveRange', {
            id: this.id
        })
    }
}

export class ShpRange {
    constructor(option) {
        let prototype = {
            id: "range_id",
            coord_type: 0,
            cad_mapkey: "",
            coord_z_type: 0,
            barrierStyle: "wave",
            color: "#ff0000",
            height: 10,
            areaStyle: "none",
            bindOn: '',
            shpName: '',
            cargo: null
        }
        this.id = option.id;
        this.data = Object.assign(prototype, option);
        this.cargo = this.data.cargo;
        let delay = 0;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        //setTimeout(() => {
        cloudRender.SuperAPI('AddShpRange', {
            id: this.data.id,
            coord_type: this.data.coord_type,
            cad_mapkey: this.data.cad_mapkey,
            coord_z_type: this.data.coord_z_type,
            type: this.data.barrierStyle,
            color: this.data.color.replace('#', ''),
            range_height: this.data.height,
            fill_area: this.data.areaStyle,
            shp_path: SHP_PATH(this.data.shpName)
        })
        //}, delay)
        //写入本次路径渲染所需的预估时间，便于后续其他元素渲染时，作为一种延时参考
        //根据测算，路径每个点的渲染时间大概是3ms
        let timeCost = 100 * 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);
        return this;
    }

    get shpName() {
        return this.data.shpName;
    }

    set shpName(value) {
        this.data.shpName = value;
        let jsondata = {
            id: this.data.id,
            coord_type: this.data.coord_type,
            cad_mapkey: this.data.cad_mapkey,
            shp_path: value
        }
        cloudRender.SuperAPI('UpdateShpRangeCoord', jsondata);
        return this;
    }


    set barrierStyle(value) {
        this.data.barrierStyle = value;
        let jsondata = {
            id: this.data.id,
            type: value,
            color: this.data.color.replace('#', ''),
            range_height: this.data.height,
            fill_area: this.data.areaStyle
        }
        cloudRender.SuperAPI('UpdateRangeStyle', jsondata);
        return this;
    }

    get barrierStyle() {
        return this.data.barrierStyle;
    }

    set color(value) {
        this.data.color = value;
        let jsondata = {
            id: this.data.id,
            type: this.data.barrierStyle,
            color: value.replace('#', ''),
            range_height: this.data.height,
            fill_area: this.data.areaStyle
        }
        cloudRender.SuperAPI('UpdateRangeStyle', jsondata);
        return this;
    }

    get color() {
        return this.data.color;
    }

    set areaStyle(value) {
        this.data.areaStyle = value;
        let jsondata = {
            id: this.data.id,
            type: this.data.barrierStyle,
            color: this.data.color.replace('#', ''),
            range_height: this.data.height,
            fill_area: value
        }
        cloudRender.SuperAPI('UpdateRangeStyle', jsondata);
        return this;
    }

    get areaStyle() {
        return this.data.areaStyle;
    }

    set height(value) {
        this.data.height = value;
        let jsondata = {
            id: this.data.id,
            type: this.data.barrierStyle,
            color: this.data.color.replace('#', ''),
            range_height: value,
            fill_area: this.data.areaStyle
        }
        cloudRender.SuperAPI('UpdateRangeStyle', jsondata);
        return this;
    }

    get height() {
        return this.data.height;
    }

    focus(distance = 200) {
        cloudRender.SuperAPI('FocusRange', {
            id: this.id,
            distance: distance
        })
    }

    remove() {
        cloudRender.SuperAPI('RemoveRange', {
            id: this.id
        })
    }
}

export class Effect {
    constructor(option) {
        this.prototype = {
            id: 'effect-id',
            coord_type: 0,
            coord_z_type: 0,
            cad_mapkey: '',
            shape: 'circle',
            radius: 0,
            elevation: 0,
            title: "",
            title_face_to_camera: true,
            coord: "0,0",
            bindOn: ''
        }
        this.data = Object.assign(this.prototype, option);
        this.cargo = option.cargo;
        let delay = 0;
        if (localStorage.lastRenderDuration) {
            delay = JSON.parse(localStorage.lastRenderDuration)
        }
        setTimeout(() => {
            cloudRender.SuperAPI('AddEffect', {
                id: this.data.id,
                coord: this.data.coord,
                coord_type: this.data.coord_type,
                cad_mapkey: this.data.cad_mapkey,
                coord_z_type: this.data.coord_z_type,
                type: this.data.shape,
                scale: this.data.radius,
                coord_z: this.data.elevation,
                title_text: this.data.title,
                title_face_to_camera: this.data.title_face_to_camera,
                rotate: 0
            })
        }, delay)
        //写入本次路径渲染所需的预估时间，便于后续其他元素渲染时，作为一种延时参考
        //根据测算，路径每个点的渲染时间大概是3ms
        let timeCost = 3;
        localStorage.lastRenderDuration = JSON.stringify(timeCost);
        return this;
    }

    get id() {
        return this.data.id;
    }

    get coord() {
        return this.data.coord;
    }

    set coord(v) {
        this.data.coord = v;
        cloudRender.SuperAPI('UpdateEffectCoord', {
            id: this.data.id,
            coord_type: this.data.coord_type,
            coord: v,
            coord_z: this.data.elevation,
            coord_z_type: 0
        })
        return this;
    }

    get elevation() {
        return this.data.elevation;
    }

    set elevation(v) {
        this.data.elevation = v;
        cloudRender.SuperAPI('UpdateEffectCoord', {
            id: this.data.id,
            coord_type: this.data.coord_type,
            coord: this.data.coord,
            coord_z: this.data.elevation,
            coord_z_type: 0
        })
        return this;
    }

    get shape() {
        return this.data.shape;
    }

    set shape(v) {
        this.data.shape = v;
        cloudRender.SuperAPI("UpdateEffectStyle", {
            id: this.data.id,
            type: this.data.shape,
            scale: this.data.radius,
            rotate: 0,
            title_text: this.data.title,
            title_face_to_camera: this.data.title_face_to_camera
        })
        return this;
    }

    get title() {
        return this.data.title;
    }

    set title(v) {
        this.data.title = v;
        cloudRender.SuperAPI("UpdateEffectStyle", {
            id: this.data.id,
            type: this.data.shape,
            scale: this.data.radius,
            rotate: 0,
            title_text: this.data.title,
            title_face_to_camera: this.data.title_face_to_camera
        })
        return this;
    }

    get radius() {
        return this.data.radius
    }

    set radius(v) {
        this.data.radius = v;
        cloudRender.SuperAPI("UpdateEffectStyle", {
            id: this.data.id,
            type: this.data.shape,
            scale: this.data.radius,
            rotate: 0,
            title_text: this.data.title,
            title_face_to_camera: this.data.title_face_to_camera
        })
        return this;
    }

    focus(distance = 200) {
        cloudRender.SuperAPI('FocusEffect', {
            id: this.id,
            distance: distance
        })
    }

    remove() {
        cloudRender.SuperAPI('RemoveEffect', {
            id: this.id
        })
    }
}

export class Viewshed {

}

export class Building {

}